package com.cyy.dyeing.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyy.dyeing.common.DataStatusEnum;
import com.cyy.dyeing.dto.MenuDto;
import com.cyy.dyeing.mapper.ModuleMapper;
import com.cyy.dyeing.model.Module;
import com.cyy.dyeing.service.ModuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: cyy
 * @Date: 2024/7/25
 */
@Service
public class ModuleServiceImpl extends ServiceImpl<ModuleMapper, Module> implements ModuleService {
    @Autowired
    private ModuleMapper moduleMapper;

    @Override
    public List<MenuDto> getRoleMenuList(Integer roId) {
        List<Module> list = moduleMapper.getRoleMenuList(roId);
        return moduleToDto(list);
    }

    @Override
    public List<MenuDto> getMenuList(String userId) {
        List<Module> list = moduleMapper.findModuleListByUid(userId, DataStatusEnum.ACTIVE.getCode());
        return moduleToDto(list);
    }

    private List<MenuDto> moduleToDto(List<Module> list){
        ArrayList<MenuDto> mainList = new ArrayList<>();
        MenuDto currentMenu = null;//主菜单
        for (Module module : list) {
            //不存在或编号不符合
            if (currentMenu == null || !currentMenu.getMenuId().equals(module.getM_pid())) {
                currentMenu = new MenuDto();
                currentMenu.setMenuId(module.getM_pid());
                currentMenu.setMenuName(module.getP_name());
                currentMenu.setSubMenuList(new ArrayList<MenuDto>());
                mainList.add(currentMenu);
            }
            MenuDto sub = new MenuDto();
            sub.setMenuId(module.getM_id());
            sub.setMenuName(module.getM_name());
            sub.setMenuUrl(module.getM_pc_url());
            sub.setHas(module.isHas());
            //将子菜单放入主菜单集合中
            currentMenu.getSubMenuList().add(sub);
        }
        return mainList;

    }
}